﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Group3.MediaTool.Core.Reactive
{
    /// <summary>
    /// @Version: 1.0
    /// @Author: Pascal Senjic
    /// @Comment:
    ///     The EntityObserver Tracks the changes DataObjects.
    ///     For more information look into the following page:
    ///     http://msdn.microsoft.com/de-de/library/vstudio/dd783449.aspx
    /// </summary>
    public class EntityObserver : IObserver<Group3.MediaTool.Interface.IObjectRelationModel>
    {
        private IDisposable unsubscriber;

        private string observername;
        public string Name { get { return observername; } }
        public Action OnCompletedAction { get; private set; }
        public Action<Exception> OnErrorAction { get; private set; }
        public Action<Group3.MediaTool.Interface.IObjectRelationModel> OnNextAction { get; private set; }
        #region IObserver
        public virtual void OnCompleted()
        {
            if (OnCompletedAction == null)
                throw new NotImplementedException();
            else
                OnCompletedAction();
        }

        public virtual void OnError(Exception error)
        {
            if (OnErrorAction == null)
                throw new NotImplementedException();
            else
                OnErrorAction(error);
        }

        public virtual void OnNext(Interface.IObjectRelationModel value)
        {
            if (OnNextAction == null)
                throw new NotImplementedException();
            else
                OnNextAction(value);
        }
        #endregion

        public virtual void Subscribe(IObservable<Group3.MediaTool.Interface.IObjectRelationModel> observable)
        {
            if (observable != null)
            {
                unsubscriber = observable.Subscribe(this);
            }
        }

        public virtual void Unsubscribe()
        {
            unsubscriber.Dispose();
        }

        public static EntityObserver CreateEntityObserver(string osn = "", Action complete = null, Action<Exception> error = null, Action<Group3.MediaTool.Interface.IObjectRelationModel> next = null)
        {
            return new EntityObserver() { observername = osn, OnCompletedAction = complete, OnErrorAction = error, OnNextAction = next };
        }
    }
}
